﻿using Cassandra;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
using System.Linq;
using System.Text;

namespace IDAdmin.Lib.DataLayer
{
    public class FilledCardWebDB
    {
        public List<DateTime> dateTimeList;
        public string tableName;
        private static Dictionary<int, string> ListRatioGosu = getListRation();
        public void transfer_FilledCard()
        {
            List<FilledCard> list = WebDB.getFilledCardData(dateTimeList);

            //using (SqlConnection conn = ConnectionHelper.GetConnection())
            //{
            //    SqlCommand sqlcommand = new SqlCommand();
            //    sqlcommand.CommandTimeout = 3600;
            //    sqlcommand.Connection = conn;
            //    sqlcommand.CommandType = CommandType.StoredProcedure;
            //    sqlcommand.CommandText = "spTransfer_delete_FilledCard";
            //    sqlcommand.Parameters.AddWithValue("@fromDT", DateTime.ParseExact(dateTimeList[0].ToString("yyyy-MM-dd") + " 00:00:00", "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture).AddHours(7).ToString("yyyy-MM-dd HH:mm:ss"));
            //    sqlcommand.Parameters.AddWithValue("@toDT", DateTime.ParseExact(dateTimeList[dateTimeList.Count - 1].ToString("yyyy-MM-dd") + " 23:59:59", "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture).AddHours(7).ToString("yyyy-MM-dd HH:mm:ss"));
            //    int affRow = sqlcommand.ExecuteNonQuery();
            //}


            foreach (FilledCard fc in list)
            {
                using (SqlConnection conn = ConnectionHelper.GetConnection())
                {
                    try
                    {
                        SqlCommand command = new SqlCommand();
                        command.Connection = conn;
                        command.CommandType = CommandType.StoredProcedure;
                        command.CommandText = "spTransfer_Insert_FilledCard";
                        command.Parameters.AddWithValue("@account", fc.ACCOUNT);
                        command.Parameters.AddWithValue("@filltime", fc.ORIGIONFILLTIME.ToString("yyyy-MM-dd HH:mm:ss"));
                        command.Parameters.AddWithValue("@point", fc.POINT);
                        command.Parameters.AddWithValue("@orderid", fc.ORDERID);
                        command.Parameters.AddWithValue("@uid", fc.UID);
                        command.Parameters.AddWithValue("@source", fc.SOURCE);
                        command.Parameters.AddWithValue("@state", fc.STATE);
                        command.Parameters.AddWithValue("@tid", fc.TID);
                        command.Parameters.AddWithValue("@type", fc.TYPE);
                        command.Parameters.AddWithValue("@tablename", tableName);
                        SqlParameter outputParam = command.Parameters.Add("@Result", SqlDbType.Int);
                        outputParam.Direction = ParameterDirection.Output;
                        command.ExecuteNonQuery();
                    }
                    catch(Exception ex)
                    {

                    }
                }
            }
        }
        public static void testtime()
        {
            List<int> typeList = new List<int> { 0 };
            try
            {
                using (ISession session = ConnectionHelper.getConnectionForCasGhStats())
                {
                    foreach (int type in typeList)
                    {
                        string cmdText = "SELECT filltime,account,point FROM filledcard";
                        try
                        {
                            RowSet rowSet = session.Execute(cmdText);
                            foreach (Row r in rowSet)
                            {
                                FilledCard fc = new FilledCard();
                                fc.FILLTIMESTR = r.GetValue<DateTime>("filltime").ToString("dd/MM/yyyy");
                                fc.FILLTIME = DateTime.ParseExact(fc.FILLTIMESTR, "dd/MM/yyyy", CultureInfo.InvariantCulture);
                                fc.ORIGIONFILLTIME = r.GetValue<DateTime>("filltime");
                                fc.POINT = r["point"].ToString();
                                fc.ACCOUNT = r["account"].ToString();
                                //result.Add(fc);
                            }
                        }
                        catch (Exception ex)
                        {
                        }
                    }

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }



        /// <summary>
        /// get list ration
        /// </summary>
        /// <returns></returns>
        public static Dictionary<int, string> getListRation()
        {
            try
            {
                Dictionary<int, string> result = new Dictionary<int, string>();
                using (SqlConnection connection = ConnectionHelper.GetConnection())
                {
                    SqlCommand cmd = new SqlCommand("select phieudiem,ratio,gosu from RatioGosu", connection);
                    SqlDataReader dataReader = cmd.ExecuteReader();
                    while (dataReader.Read())
                    {
                        result.Add(Convert.ToInt32(dataReader["phieudiem"]), dataReader["gosu"].ToString());
                    }
                    dataReader.Close();
                }
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static int getRation(int key)
        {
            string valueRation = "0";
            ListRatioGosu.TryGetValue(key, out valueRation);
            return Convert.ToInt32(valueRation);
        }
    }


}
